package com.stevens.triangles;

import java.util.Arrays;
import java.util.Scanner;

public class RobTriangles extends TrianglesBase {
	
	private static final int NUM_SIDES = 3;
	private static final double EPSILON = 0.02;
	
	public RobTriangles(double[] triangleSides) {
		super(triangleSides);
	}
	
	@Override
	public String kindOfTriangle() {
		if (this.triangleSides.length < NUM_SIDES) {
			return kTooFewSidesError;
		} else if (this.triangleSides.length > NUM_SIDES) {
			return kTooManySidesError;
		}
		
		// check for negative or zero sides
		for (double sideLength: this.triangleSides) {
			if (sideLength <= 0.0) {
				return kInvalidSideError;
			}
		}
		
		// check A + B > C condition
		Arrays.sort(this.triangleSides);
		if (this.triangleSides[0] + this.triangleSides[1] <= this.triangleSides[2])
			return kInvalidTriangleError;
		
		// determine number of sides which are equal, to differentiate scalene, isosceles, and equilateral
		int numSidesEqual = 0;
		if (this.triangleSides[0] == this.triangleSides[1])
			numSidesEqual++;
		if (this.triangleSides[0] == this.triangleSides[2])
			numSidesEqual++;
		if (this.triangleSides[1] == this.triangleSides[2])
			numSidesEqual++;
		
		// check to see if sides obey pythagorean theorem, implying it is a right triangle
		boolean rightTriangle = false;		
		// A^2 + B^2 == C^2 for right trangles, where C is the largest side
		// subtraction of two sides is done so we can find result within an error range
		double difference = Math.abs(this.triangleSides[0]*this.triangleSides[0] + 
			     this.triangleSides[1]*this.triangleSides[1] -
			     this.triangleSides[2]*this.triangleSides[2]);
		System.out.println(difference);
		if (difference <= EPSILON)
			rightTriangle = true;
		
		// output name of triangle type
		switch (numSidesEqual) {
		case 0:
			return (rightTriangle) ? kScaleneRightTriangle : kScaleneTriangle;
		case 1:
			return (rightTriangle) ? kIsoscelesRightTriangle : kIsoscelesTriangle;
		case 2:
			return kTwoDistinctEqualPairsError;
		case 3:
			return kEquilateralTriangle;
		default:
			return "ERROR: Triangle has " + numSidesEqual + " equal sides, which should never happen.";
		}
	}
	
	public static void main(String[] args) {
		System.out.println("RobTriangles v1.1");
		Scanner scan = new Scanner(System.in);
		double[] sideLengths = new double[NUM_SIDES];
		for (int i = 0; i < NUM_SIDES; i++) {
			System.out.println("Enter length of side " + i + ": ");
			sideLengths[i] = scan.nextDouble();
		}
		RobTriangles tri = new RobTriangles(sideLengths);
		System.out.println(tri.kindOfTriangle());
	}

	
}
